﻿<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Dialog extends MY_Controller
{
    /** @var \User */
    private $CurrentUser;
    /** @var string */
    private $Html;

    public function __construct()
    {
        parent::__construct();

        // 检查状态
        YimeDevTools::checkSiteStatus();

        $this->CurrentUser = UserManager::getCurrentUserBySession();

        $this->load->vars(array(
            'CurrentUser' => $this->CurrentUser,
        ));

        $this->Html = '';
    }

    public function login()
    {
        //weibo get user_key
        $o  = SaeTOAuthV2::Init(WB_AKEY , WB_SKEY);
        $code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );

        $this->load->vars(array(
            'ModalTitle' => '登录',
            'ModalBody'  => $this->load->view('dialog/login', array('Weibo_URL' => $code_url), true),
            
        ));

        $this->Html .= $this->load->view('dialog', array(), true);

        echo json_encode(array(
            'type' => 'yime-modal',
            'html' => $this->Html
        ));
    }

    public function register()
    {
        $this->load->helper('captcha');
        $data    = array(
            'word'       => random_string('alnum', 4),
            'img_path'   => './public/captcha/',
            'img_url'    => base_url('public/captcha') . '/',
            'font_path'  => './public/font/arialbd.ttf',
            'img_width'  => 160,
            'img_height' => 43,
        );
        $captcha = create_captcha($data);
        try {
            Captcha_Model::update($captcha);
        } catch (Exception $ex) {
            $captcha = array('image' => '<p class="text-danger text-center" style="line-height: 40px;background: #fc8;">验证码生成出错</p>');
        }

        $this->load->vars(array(
            'ModalTitle' => '新用户注册',
            'ModalBody'  => $this->load->view('dialog/register', array('captcha' => $captcha), true),
        ));

        $this->Html .= $this->load->view('dialog', array(), true);

        echo json_encode(array(
            'type' => 'yime-modal',
            'html' => $this->Html
        ));
    }

    public function wantToSell()
    {
        if ($this->CurrentUser->getId() == 0) {
            $this->login();
            return;
        }
        if($this->CurrentUser->getStudentInfoId() == '' || $this->CurrentUser->getStudentInfoId()==null)
        {
            $this->studentRealNameAuth();
            return;
        }elseif($this->CurrentUser->getPower()<1){
            echo json_encode(array(
                'type' => 'nopower',
                'html' => base_url('home/userCenter#resendemail'),
            ));
            return;
        }else{
            $this->load->vars(array(
                'ModalTitle' => '发布出售',
                'ModalBody'  => $this->load->view('dialog/want-to-sell', array(
                        'MasterCommodityCategoryArray'    => CommodityCategory_Model::getMasterCommodityCategoryArray(),
                        'NotMasterCommodityCategoryArray' => CommodityCategory_Model::getNotMasterCommodityCategoryArray(),
                    ), true),
            ));

            $this->Html .= $this->load->view('dialog-lg', array(), true);

            echo json_encode(array(
                'type' => 'bootstrap-modal',
                'html' => $this->Html
            ));
        }
    }

    public function wantToBuy()
    {
        if ($this->CurrentUser->getId() == 0) {
            $this->login();
            return;
        }

        if($this->CurrentUser->getStudentInfoId() == '' || $this->CurrentUser->getStudentInfoId()==null)
        {
            $this->studentRealNameAuth();
            return;
        }elseif($this->CurrentUser->getPower()<1){
            echo json_encode(array(
                'type' => 'nopower',
                'html' => base_url('home/userCenter#resendemail'),
            ));
            return;
        }else{
            $this->load->vars(array(
                'ModalTitle' => '发布求购',
                'ModalBody'  => $this->load->view('dialog/want-to-buy', array(
                        'MasterCommodityCategoryArray'    => CommodityCategory_Model::getMasterCommodityCategoryArray(),
                        'NotMasterCommodityCategoryArray' => CommodityCategory_Model::getNotMasterCommodityCategoryArray(),
                    ), true),
            ));

            $this->Html .= $this->load->view('dialog-lg', array(), true);

            echo json_encode(array(
                'type' => 'bootstrap-modal',
                'html' => $this->Html
            ));
        }
    }

    public function editCommodity($commodityId = 0)
    {
        if ($this->CurrentUser->getId() == 0) {
            $this->login();
            return;
        }

        $this->load->vars(array(
            'ModalTitle' => '编辑商品',
            'ModalBody'  => $this->load->view('dialog/edit-commodity', array(
                'MasterCommodityCategoryArray'    => CommodityCategory_Model::getMasterCommodityCategoryArray(),
                'NotMasterCommodityCategoryArray' => CommodityCategory_Model::getNotMasterCommodityCategoryArray(),
                'thatCommodity'          => Commodity_Model::getCommodityById($commodityId),
            ), true),
        ));
        $this->Html .= $this->load->view('dialog-lg', array(), true);

        echo json_encode(array(
            'type' => 'bootstrap-modal',
            'html' => $this->Html
        ));
    }

    public function editRequire($commodityId = 0)
    {
        if ($this->CurrentUser->getId() == 0) {
            $this->login();
            return;
        }

        $this->load->vars(array(
            'ModalTitle' => '编辑求购',
            'ModalBody'  => $this->load->view('dialog/edit-require', array(
                'MasterCommodityCategoryArray'    => CommodityCategory_Model::getMasterCommodityCategoryArray(),
                'NotMasterCommodityCategoryArray' => CommodityCategory_Model::getNotMasterCommodityCategoryArray(),
                'thatRequire'                     => CommodityRequire_Model::getCommodityRequireById($commodityId),
            ), true),
        ));
        $this->Html .= $this->load->view('dialog-lg', array(), true);

        echo json_encode(array(
            'type' => 'bootstrap-modal',
            'html' => $this->Html
        ));
    }

    public function addSchool()
    {
        if ($this->CurrentUser->getId() == 0) {
            $this->login();
            return;
        }

        $this->load->vars(array(
            'ModalTitle' => '添加学校',
            'ModalBody'  => $this->load->view('dialog/add-school', array(), true),
        ));
        $this->Html .= $this->load->view('dialog-lg', array(), true);

        echo json_encode(array(
            'type' => 'bootstrap-modal',
            'html' => $this->Html
        ));
    }

    public function studentRealNameAuth()
    {
        if ($this->CurrentUser->getId() == 0) {
            $this->login();
            return;
        }

        $this->load->vars(array(
            'ModalTitle' => '实名认证',
            'ModalBody'  => $this->load->view('dialog/student-real-name-auth', array(
                'thatUserId'  => $this->CurrentUser->getId(),
                'SchoolArray' => School_Model::getSchoolArray(),
            ), true),
        ));
        $this->Html .= $this->load->view('dialog', array(), true);

        echo json_encode(array(
            'type' => 'bootstrap-modal',
            'html' => $this->Html
        ));
    }

    public function feedback()
    {
        $this->load->vars(array(
            'ModalTitle' => '反馈意见',
            'ModalBody'  => $this->load->view('dialog/feedback', array(
                'thatUserId' => $this->CurrentUser->getId(),
            ), true),
        ));
        $this->Html .= $this->load->view('dialog', array(), true);

        echo json_encode(array(
            'type' => 'bootstrap-modal',
            'html' => $this->Html
        ));
    }
	
    public function changeschool()
    {
        $this->load->vars(array(
            'ModalTitle' => '请选择学校',
            'ModalBody'  => $this->load->view('dialog/change-school', array(
                    'thatUserId' => $this->CurrentUser->getId(),
                    'schoolList'=>School_Model::getSchoolArray()
            ), true),
        ));
        $this->Html .= $this->load->view('dialog-lg', array(), true);

        echo json_encode(array(
            'type' => 'bootstrap-modal',
            'html' => $this->Html
        ));
    }

    public function uploadFile()
    {
        if ($this->CurrentUser->getId() == 0) {
            $this->login();
            return;
        }

        $this->Html .= $this->load->view('home/html-header', array(), true);
        $this->Html .= $this->load->view('dialog/upload-file', array(), true);
        $this->Html .= $this->load->view('home/html-footer', array(), true);

        echo json_encode(array(
            'type' => 'bootstrap-modal',
            'html' => $this->Html
        ));
    }

}